package com.droid.library.widget.edittext;

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

/**
 * EditText基类
 *
 * @author wangheng
 */

public class BaseEditText extends AppCompatEditText {

    private OnMockClickListener mOnMockClickListener = null;
    private long mActionDownTime = 0;

    public BaseEditText(Context context) {
        super(context);
    }

    public BaseEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private void setOnMockClickListener(OnMockClickListener listener){
        mOnMockClickListener = listener;
    }

    public interface OnMockClickListener{
        void onClick(View view);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mActionDownTime = System.currentTimeMillis();
                break;

            case MotionEvent.ACTION_UP:
                if(System.currentTimeMillis() - mActionDownTime < ViewConfiguration.getLongPressTimeout()
                        && mOnMockClickListener != null){
                    mOnMockClickListener.onClick(this);
                }
                break;
            default:
                // nothing to do
                break;

        }
        return super.onTouchEvent(event);
    }
}
